package com.example.manager.service;

import com.example.common.Ov.QuotaApplicationOv;
import com.example.common.entity.PageParam;
import com.example.common.entity.Rsp;
import com.example.common.pojo.QuotaApplication;
import com.example.manager.mapper.QuotaApplicationMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class QuotaApplicationService {

    @Autowired
    private QuotaApplicationMapper quotaApplicationMapper;

    /**
     * 列表查询
     * @param pageParam
     * @return
     */
    public Rsp getQuotaList(PageParam<QuotaApplicationOv> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行带查询条件的高级检索
            //开启分页插件
            PageHelper.startPage(pageParam.getPageNo(), pageParam.getPageSize());
            List<QuotaApplicationOv> quotaApplicationOvs = quotaApplicationMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<QuotaApplicationOv> pageInfo = new PageInfo<>(quotaApplicationOvs);
            return new Rsp(20000, pageInfo);

        } else {
            //执行不带条件的列表查询
            PageHelper.startPage(pageParam.getPageNo(), pageParam.getPageSize());
            List<QuotaApplicationOv> allList = quotaApplicationMapper.getAllList();
            PageInfo<QuotaApplicationOv> pageInfo = new PageInfo<>(allList);
            return new Rsp(20000, pageInfo);
        }


    }

    /**
     * 借款审核通过
     * @param quotaApplication
     * @return
     */
    public Rsp updateStatus(QuotaApplication quotaApplication){
        quotaApplication.setStatus("1");
        int i = quotaApplicationMapper.updateByPrimaryKeySelective(quotaApplication);
        if(i>0){
            return new Rsp(20000,"审核成功");
        }else {
            return new Rsp(50000,"审核失败");
        }
    }

    /**
     * 借款审核不通过，已驳回
     * @param quotaApplication
     * @return
     */
    public Rsp updateStatusBack(QuotaApplication quotaApplication){
        quotaApplication.setStatus("2");
        int i = quotaApplicationMapper.updateByPrimaryKeySelective(quotaApplication);
        if(i>0){
            return new Rsp(20000,"审核驳回成功");
        }else {
            return new Rsp(50000,"审核驳回失败");
        }
    }
}
